<template>
  <div class="app-container">
    <el-form :inline="true" size="small">
      <el-form-item label="搜索:">
        <el-input v-model="searchParams.search" clearable placeholder="请输入搜索内容" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="fetchData">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" icon="el-icon-delete" @click="clearLogs">清空日志</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="ID">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户名">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="日志内容">
        <template slot-scope="scope">
          {{ scope.row.content }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="日志时间">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div :style="{textAlign:'center',padding:'10px'}">
      <el-pagination
        background
        :current-page.sync="searchParams.pageNum"
        :page-sizes="[10, 20, 30, 40,50]"
        :page-size.sync="searchParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="searchParams.total"
        @size-change="fetchData"
        @current-change="fetchData"
      />
    </div>
  </div>
</template>

<script>
import { getLogs, delLog, cleanAllLogs } from '@/api/system/logs'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      formVisible: false,
      form: {},
      searchParams: {
        pageNum: 1,
        pageSize: 10,
        search: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    clearLogs() {
      this.$confirm('确认清空所有的日志信息吗？', '提示').then(() => {
        cleanAllLogs().then(() => {
          this.$message.success('清空日志成功')
          this.fetchData()
        })
      })
    },
    handleDelete({ id }) {
      this.$confirm('确认删除此信息吗？', '警告', {
        cancelButtonText: '取消',
        confirmButtonText: '确认'
      }).then(() => {
        delLog(id).then(() => {
          this.$message.success('删除成功')
          this.fetchData()
        })
      })
    },
    fetchData() {
      this.listLoading = true
      getLogs(this.searchParams).then(response => {
        this.list = response.data.records
        this.searchParams.total = response.data.total
        this.listLoading = false
      })
    }
  }
}
</script>

